function setWH(selector) {
	winwidth=document.all?document.body.clientWidth:window.innerWidth; 
	winHeight=document.all?document.body.clientHeight:window.innerHeight;
	$(selector).css({width:(winwidth)+"px",height:(winHeight)+"px"});
}
var nominal = 0.2;
var sliderClientX;
var slideStart = false;
var Viewer;
$(document).ready(function() {
	setWH("#diter");
	$(window).resize(function(){
		setWH("#diter");
	});
	Viewer = new PanoradoJS(document.getElementById("diter"));
	Viewer.panCursor0 = "http://project.tk/images/diter/Pan0.cur";
	Viewer.panCursor1 = "http://project.tk/images/diter/Pan0.cur";
	Viewer.fullWindowEnabled = false;
	Viewer.imageList = imgList;
	Viewer.controls.visible = false;
	Viewer.selectImage(0);
	Viewer.onDblClick =	function(event) {
		Viewer.zoomToCursor(2);
	};
	self.setInterval(function(){checkTable();},1000);
});
$(window).mousemove(function (event){
	if( !slideStart ) {
		return;
	}
	sliderClientX = event.clientX - $("#slider").offset().left +15;
	window.controlSlider();
});
$("#slider").mousedown(function (event) { slideStart = true;
	sliderClientX = event.clientX - $("#slider").offset().left +15;
	window.controlSlider();
});
$(window).mouseup(function () { slideStart = false;});

$('#rezerDate').datepicker({
	minDate: 0,                    
	beforeShowDay: noSunday
});
function noSunday(date){
	var day = date.getDay();
	return [(day > 0), ''];
}
function controlSlider(){
	var slider = $("#slider")[0];
	for( var i=0; i<slider.children.length; i++ ) {
		if( slider.children[i].offsetLeft<=sliderClientX && (slider.children[i].offsetLeft+slider.children[i].clientWidth)>sliderClientX) {
			if( $(slider.children[i]).hasClass("bar-success") ) {
				var obj = {x:sliderClientX,width:slider.children[i].clientWidth,offsetLeft:slider.children[i].offsetLeft};
				controlTime(obj);
			}
		}
	}
}
function controlTime(obj){
	var slideWidth = $("#slider").width();
	var nominalWidth = slideWidth * nominal; 
	var waitWidth = obj.width - (obj.x - obj.offsetLeft);
	if( waitWidth > nominalWidth ) {
		waitWidth = nominalWidth;
	}
	//var w = parseInt((waitWidth/slideWidth )*100) + "%";
	//obj.x = parseInt((obj.x/slideWidth )*100) + "%";
	$("#waitTime").css({left:obj.x,width:waitWidth});
}
function order(id) {
	var name = IDtoTitle[id];
	$("#tableTitle").text(name);
	$("#order").modal("show");
}
function checkTable() {
	$.remoteCall('USER_tables','checkChageTable',[hallID],onCheckTable);
	self.setInterval(function(){checkTable();},10000);
}
function onCheckTable() {
	var res = [];
	res = this.result;
	if( res.length >0 ) {
		for( var i=0; i<res.length; i++ ) {
			var images = "";
			if( res[i].status == 0 ) {
				images = imgFree;
			} else {
				images = imgBusy;
			}
			var index = IDtoIndex[res[i].id];
			Viewer.image.hotspots[index].img = images;
		}
	}
}
